package com.spoj;

import java.util.Scanner;
import java.util.Stack;
public class CMEXPR{
    String expr;
    String op;
    public CMEXPR(String expr, String op){
        this.expr = expr;
        this.op = op;
    }
    public static void main(String[] args){
        try{
            Scanner input = new Scanner(System.in);
            int number = input.nextInt();
            for (int i=0; i<number; i++){
                String exp = input.next();
                String postfix = toPrefix(exp);
               //System.out.println(postfix);
               // System.out.println(toPrefix(exp));
                System.out.println(postToIn(postfix));
            }
            input.close();
        }catch(Exception e){
            e.printStackTrace();
            return;
        }
    }
    public static String postToIn(String exp){
        Stack<CMEXPR> tempStack = new Stack<CMEXPR>();
        String temp = "";
        for(int j=0; j<exp.length(); j++){
            temp = Character.toString(exp.charAt(j));
            if("+".equals(temp) || "-".equals(temp)){
                CMEXPR right = tempStack.pop();
                CMEXPR left = tempStack.pop();
                if("-".equals(temp)){
                    if("+".equals(right.op)|| "-".equals(right.op)){
                        right.expr = "(" + right.expr + ")";
                    }
                }
                String newExp = left.expr + temp + right.expr;
                tempStack.push(new CMEXPR(newExp, temp));
            }else if("*".equals(temp) || "/".equals(temp)){
                CMEXPR right = tempStack.pop();
                CMEXPR left = tempStack.pop();
                if("+".equals(left.op)|| "-".equals(left.op)){
                    left.expr = "(" + left.expr + ")";
                }
                if("+".equals(right.op)|| "-".equals(right.op)){
                    right.expr = "(" + right.expr + ")";
                }
                if("/".equals(temp)){
                    if("/".equals(right.op) || "*".equals(right.op)){
                        right.expr = "(" + right.expr + ")"; 
                    }
                }
                
                String newExp = left.expr + temp + right.expr;
                tempStack.push(new CMEXPR(newExp, temp));
            }else{
                tempStack.push(new CMEXPR(temp, ""));
            }
        }
        return tempStack.pop().expr;
    }
    static Stack operatorStack = new Stack();
    public static String toPrefix(String expression) {
        expression="("+expression+")";
        int i;
        char token;
       // String output = "";
        StringBuilder sb = new StringBuilder();
        for (i = 0; i < expression.length(); i++) {
            token = expression.charAt(i);
            if (Character.isLetterOrDigit(token) == true)
                sb.append(token);
               // output += token;
            else if (token == '(')
                operatorStack.push(token);
            else if (token == ')') {
                char seeTop;
                while ((seeTop = seeTop()) != '(') {
                    sb.append(seeTop);
                    //output += seeTop;
                    popSeeTop();
                }

                operatorStack.pop();
            } else {
                while (priority(token) <= priority(seeTop())) {
                    sb.append(seeTop());
                    //output += seeTop();
                    popSeeTop();
                }
                operatorStack.push(token);
            }
        }
        return sb.toString();

    }

    private static int priority(char operator) {
        if (operator == '^')
            return 3;
        if (operator == '/' || operator == '*')
            return 2;
        if (operator == '+' || operator == '-')
            return 1;
        return 0;
    }

    private static Character seeTop() {
        if(!operatorStack.empty())
            return (Character) operatorStack.peek();
        else
            return '0';
    }

    private static void popSeeTop() {
        if(!operatorStack.empty())
            operatorStack.pop();
    }
}
